package com.tianhui.consignortwofei.mvp.ui.viewHolder.detail;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.utils.CommonUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.DetailItemAdapter;

import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;
import org.yczbj.ycrefreshviewlib.item.RecycleViewItemLine;

/**
 * @author fgs
 * @date 2020/8/28
 * @description
 **/
public class DetailViewHolder extends BaseViewHolder<DetailInfo> {

    private LinearLayout titleLinearLayout;
    private View titleTipView;
    private TextView titleTextView;
    private TextView operateTextView;
    private LinearLayout topLinearLayout;
    private TextView topLeftTextView;
    private TextView topMiddleTextView;
    private TextView topRightTextView;
    private View bottomLineView;
    private LinearLayout bottomLinearLayout;
    private TextView bottomLeftTextView;
    private TextView bottomMiddleTextView;
    private TextView bottomRightTextView;


    private RecyclerView detailRecyclerView;
    private DetailItemAdapter mItemAdapter;

    public DetailViewHolder(ViewGroup parent, int res) {
        super(parent, res);
        titleLinearLayout = getView(R.id.layout_item_detail_title_linearLayout);
        titleTipView = getView(R.id.layout_item_detail_title_titleTipView);
        titleTextView = getView(R.id.layout_item_detail_title_titleTextView);
        operateTextView = getView(R.id.item_detail_operateTextView);
        detailRecyclerView = getView(R.id.item_detail_item_recyclerView);

        topLinearLayout = getView(R.id.item_detail_topLinearLayout);
        topLeftTextView = getView(R.id.item_detail_topLeftTextView);
        topMiddleTextView = getView(R.id.item_detail_topMiddleTextView);
        topRightTextView = getView(R.id.item_detail_topRightTextView);

        bottomLineView = getView(R.id.item_detail_bottomLine);
        bottomLinearLayout = getView(R.id.item_detail_bottomLinearLayout);
        bottomLeftTextView = getView(R.id.item_detail_bottomLeftTextView);
        bottomMiddleTextView = getView(R.id.item_detail_bottomMiddleTextView);
        bottomRightTextView = getView(R.id.item_detail_bottomRightTextView);




        RecycleViewItemLine itemLine = new RecycleViewItemLine(getContext(),
                LinearLayout.HORIZONTAL,
                (int) CommonUtil.convertDpToPixel(10),
                getContext().getResources().getColor(R.color.colorGrayBackground));
//        detailRecyclerView.addItemDecoration(itemLine);
        addOnClickListener(R.id.item_detail_operateTextView);

    }

    @Override
    public void setData(DetailInfo data) {
        super.setData(data);



        if (data.isShowTitleTip) {
            titleTipView.setVisibility(View.VISIBLE);
            titleTextView.setTextColor(getContext().getResources().getColor(R.color.colorBlack));
            setTitleLinearLayoutPadding(10, 10, 10, 10);
        } else {
            titleTipView.setVisibility(View.GONE);
            titleTextView.setTextColor(getContext().getResources().getColor(R.color.colorTextPrimary));
            setTitleLinearLayoutPadding(10, 5, 10, 5);
        }
        if (!TextUtils.isEmpty(data.title)) {
            titleTextView.setText(data.title);
            titleTextView.setVisibility(View.VISIBLE);
        } else {
            titleTextView.setVisibility(View.GONE);
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), data.spanCount);
        detailRecyclerView.setLayoutManager(gridLayoutManager);
        mItemAdapter = new DetailItemAdapter(getContext());
        detailRecyclerView.setAdapter(mItemAdapter);
        mItemAdapter.addAll(data.infoList);


        if (!TextUtils.isEmpty(data.operateText)) {
            operateTextView.setText(data.operateText);
            operateTextView.setVisibility(View.VISIBLE);
        } else {
            operateTextView.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(data.topLeftText) || !TextUtils.isEmpty(data.topMiddleText) || !TextUtils.isEmpty(data.topRightText)) {
            topLinearLayout.setVisibility(View.VISIBLE);

            topLeftTextView.setText(data.topLeftText);

            if (!TextUtils.isEmpty(data.topMiddleText)) {
                topMiddleTextView.setText(Html.fromHtml(data.topMiddleText));
                topMiddleTextView.setVisibility(View.VISIBLE);
            } else {
                topMiddleTextView.setVisibility(View.INVISIBLE);
                topMiddleTextView.setText("");
            }

            if (!TextUtils.isEmpty(data.topRightText)) {
                topRightTextView.setText(data.topRightText);
                topRightTextView.setVisibility(View.VISIBLE);
            } else {
                topRightTextView.setVisibility(View.GONE);
            }


        } else {
            topLinearLayout.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(data.bottomLeftText) || !TextUtils.isEmpty(data.bottomMiddleText) || !TextUtils.isEmpty(data.bottomRightText)) {
            bottomLinearLayout.setVisibility(View.VISIBLE);

            bottomLineView.setVisibility(View.VISIBLE);
            bottomLeftTextView.setText(data.bottomLeftText);

            if (!TextUtils.isEmpty(data.bottomMiddleText)) {
                bottomMiddleTextView.setText(data.bottomMiddleText);
                bottomMiddleTextView.setVisibility(View.VISIBLE);
            } else {
                bottomMiddleTextView.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(data.bottomRightText)) {
                bottomRightTextView.setText(data.bottomRightText);
                bottomRightTextView.setVisibility(View.VISIBLE);
            } else {
                bottomRightTextView.setVisibility(View.GONE);
            }
        } else {
            bottomLineView.setVisibility(View.GONE);
            bottomLinearLayout.setVisibility(View.GONE);
        }

    }

    private void setTitleLinearLayoutPadding(int left, int top, int right, int bottom) {
        titleLinearLayout.setPadding((int) CommonUtil.convertDpToPixel(left), (int) CommonUtil.convertDpToPixel(top), (int) CommonUtil.convertDpToPixel(right), (int) CommonUtil.convertDpToPixel(bottom));
    }
}
